package main.hw1model;

import java.util.List;
import java.io.Serializable;

import javax.persistence.*;

import main.util.HibernateContext;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Example;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

@Entity
@Table(name="airportdesc")
public class AirportDesc implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4489876102974476811L;
	
	private int descId;
	private String origin;
	private String originCityName;
	private String originStateAbr;
	private Airport airport;
	
	public AirportDesc() {}
	
	public AirportDesc(String origin, String originCityName, String originStateAbr) {
		this.origin = origin;
		this.originCityName = originCityName;
		this.originStateAbr = originStateAbr;
	}
	
	public AirportDesc(Airport airport, String origin, String originCityName, String originStateAbr) {
		this.origin = origin;
		this.originCityName = originCityName;
		this.originStateAbr = originStateAbr;
		this.airport = airport;
	}
	
	@Id
	@GeneratedValue
	@Column(name="DESC_ID")
	public int getDescId() {
		return descId;
	}
	public void setDescId(int descId) {
		this.descId = descId;
	}
	
	@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
	@JoinColumn(name="AIRPORT_ID")
	public Airport getAirport() {
		return airport;
	}
	public void setAirport(Airport airport) {
		this.airport = airport;
	}
	
	@Column(name="ORIGIN")
	public String getOrigin() {
		return origin;
	}
	public void setOrigin(String origin) {
		this.origin = origin;
	}
	
	@Column(name="ORIGIN_CITY_NAME")
	public String getOriginCityName() {
		return originCityName;
	}
	public void setOriginCityName(String originCityName) {
		this.originCityName = originCityName;
	}

	@Column(name="ORIGIN_STATE_ABR")
	public String getOriginStateAbr() {
		return originStateAbr;
	}
	public void setOriginStateAbr(String originStateAbr) {
		this.originStateAbr = originStateAbr;
	}
	
	/**
	 *load the airport description data into table
	 */
	public static void load() {
		Session session = HibernateContext.getSession();
		Transaction tx = session.beginTransaction();
		session.save(new AirportDesc(new Airport("Jerry International Airport"), "Jerry5", "RacoonCity", "JW"));
		session.save(new AirportDesc(new Airport("SJSU Airport"), "Jerry6", "DogTown", "JW"));
		session.save(new AirportDesc(new Airport("San Francisco Interantional Airport"), "Jerry7", "San Francisco", "JW"));
		session.save(new AirportDesc(new Airport("San Jose Regional Airport"), "Jerry8", "SanJose", "JW"));
		tx.commit();
		session.close();
		
		System.out.println("AirportDesc Data Loaded");
	}
	
	/**
	 * retrieve the airport description by criteria
	 * @return All Data in AirportDesc Table
	 */
	public static List<AirportDesc> retrieveAll() {
		Session session = HibernateContext.getSession();
		Criteria criteria = session.createCriteria(AirportDesc.class);
		List<AirportDesc> result = (List<AirportDesc>) criteria.list();
		
		session.close();
		return result;
	}
	
	/**
	 * Find Specific Airport Description using QBE
	 * @param condition specific original city name to find airport description
	 * @return unique airport description by condition
	 */
	public static AirportDesc find(String condition) {
		AirportDesc result = new AirportDesc();
		
		AirportDesc prototype = new AirportDesc();
		prototype.setOriginCityName(condition);
		Example example = Example.create(prototype);
		
		try {
			Session session = HibernateContext.getSession();
			Criteria criteria = session.createCriteria(AirportDesc.class);
			criteria.add(example);
			result = (AirportDesc) criteria.uniqueResult();
			session.close();
		} catch (Exception e) {
			System.err.println("Error in AirportDesc.find()");
			e.printStackTrace();
		}
		
		return result;
	}
}
